//
//  HeartHistoryViewModel.swift
//  MoyoungHome
//
//  Created by 易先生 on 2022/9/15.
//

import UIKit
import RxCocoa
import MYModule

public enum HistoryShowType {
    case bloodOxygen, stress, hrv,heart
}

open class HistoryViewModel: NSObject {

	public var sections: [HistorySectionModel] = []

    public let historysRelay = BehaviorRelay<Bool>(value: false)

	private var showType: HistoryShowType = .bloodOxygen

	public init(showType: HistoryShowType) {
		super.init()
		self.showType = showType
		getHistoryData()
	}

	private func getHistoryData() {
		switch showType {
            
		case .bloodOxygen:
            
        let all = [SingleBloodOxygenModel].descAll()
            
          sections = all.map {
              
              let items = $0.filter { model in model.o2 > 0 }
              
              let title = $0.first?.date.localString
              return HistorySectionModel(title: title, items: items)
          }
            
        case .hrv:
            let all = [SingleHRVModel].descAll()
                
              sections = all.map {
                  
                  
                  let items = $0.filter { model in model.hrv > 0 }
                  
                  let title = $0.first?.date.localString
                  return HistorySectionModel(title: title, items: items)
              }
            
        case .stress:
            let all = [SingleStressModel].descAll()
                
              sections = all.map {
                  
                  let items = $0.filter { model in model.stress > 0 }
                  
                  let title = $0.first?.date.localString
                  return HistorySectionModel(title: title, items: items)
              }
            
        case .heart:
            let all = [SingleHeartRateModel].descAll()
                
              sections = all.map {
                  
                  let items = $0.filter { model in model.heartRate > 0 }
                  
                  let title = $0.first?.date.localString
                  return HistorySectionModel(title: title, items: items)
              }
            
        }
        
        sections = sections.filter({ !$0.items.isEmpty })
        
        historysRelay.accept(!sections.isEmpty)
	}

//	public func deleteHeart(indexPath: IndexPath) {
//        let model = datas[indexPath.section].items[indexPath.row]
//        let rowid = model.id
//        switch showType {
//        case .bloodOxygen:
//            print("删除操作\(rowid)")
//        case .hrv:
//            print("删除操作\(rowid)")
//        case .stree:
//            print("删除操作\(rowid)")
//        }
//	}
}
